package cn.com.pansky.comm.interceptor;

import java.util.HashSet;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import net.vsame.url2sql.helper.Url2SqlContext;
import net.vsame.url2sql.helper.WebHelper;
import net.vsame.url2sql.url.Interceptor;
import net.vsame.url2sql.url.impl.UrlMapping;
import net.vsame.url2sql.url.impl.UrlMapping.Chain;


public class SessionInterceptor implements Interceptor {

	private static Set<String> set = new HashSet<String>(); 

	static {
		
		set.add("/myAccount/login/checkStatus");
		set.add("/myAccount/login/checkUser_web");
		set.add("/myAccount/register/checkCardId");
		set.add("/myAccount/register/checkPhoneNum");
		set.add("/myAccount/register/saveUser");
		set.add("/myAccount/forgetPass/isUser");
		set.add("/myAccount/forgetPass/getCode");
		set.add("/myAccount/forgetPass/validateCode");
		set.add("/myAccount/forgetPass/updPass");
		set.add("/myAccount/login/checkUser");
		set.add("/myAccount/login/sendCode");
		set.add("/myAccount/login/codeLogin");
		
		
		set.add("/dict/organization");
		set.add("/dict/streetTown");
		set.add("/dict/village");
		
		//单位用户管理
		set.add("/unit/user/reg");
		set.add("/unit/user/sendCode");
		set.add("/unit/user/codeLogin");
		set.add("/unit/user/login");
		
		set.add("/notice/pushToAll");
		set.add("/notice/pushToSingle");
		
		//单位版注册通知
		set.add("/outer/user/sendMsg");
		
		//待遇测算
		set.add("/myAccount/receiveTest");
		// 银行退款
		set.add("/bank/bankRefund");
		// 对账
		set.add("/bank/bankStatement");
		
		//职工触摸屏过来的认证
		set.add("/zgss/auth"); 
		set.add("/zgss/isNeedAuth"); 
		set.add("/zgss/getPersonInfo"); 
		

		//新注册
		set.add("/myAccount/register/saveUserInfo");
		set.add("/myAccount/register/checkCode");
		
		
		set.add("/xxxx/test");
		set.add("/outer/msg/commMsg");
	}
	
	@Override
	public void init(UrlMapping urlMapping, ServletContext servletContext) {
		
	}

	@Override
	public void invoke(Chain chain) {
		Url2SqlContext c = WebHelper.getContext();
		HttpSession session = c.getServletSession();
		if(set.contains(c.getRequestUrl())) {
			chain.next();
		}else {
			if(session.getAttribute("user") == null) {
//				String token = c.getParam("token");
//				UserService us = new UserService();
//				us.autoLogin(token);
//				chain.next();
				c.putError(1, "请登录");
			} else {
				chain.next();
			}
		}
	}

	@Override
	public void destroy() {
	}
}
